import 'dart:convert' show json;

import 'package:loxen/mode/inspiration/inspiration_info_bean.dart';

import 'material_item.dart';

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }

  return null;
}

class MaterialInfo {
  MaterialInfo({
    this.id,
    this.cover,
    this.code,
    this.layer,
    this.size,
    this.biaomianjili,
    this.view_num,
    this.like_num,
    this.collect_num,
    this.related,
    this.name,
    this.article,
    this.image,
    this.story,
    this.is_like,
    this.is_collect,
    this.brand,
  });

  factory MaterialInfo.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<MaterialItem> related =
        jsonRes['related'] is List ? <MaterialItem>[] : null;
    if (related != null) {
      for (final dynamic item in jsonRes['related']) {
        if (item != null) {
          related.add(MaterialItem.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }

    final List<InspirationInfo> article =
        jsonRes['article'] is List ? <InspirationInfo>[] : null;
    if (article != null) {
      for (final dynamic item in jsonRes['article']) {
        if (item != null) {
          article
              .add(InspirationInfo.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }
    return MaterialInfo(
      id: asT<int>(jsonRes['id']),
      cover: asT<String>(jsonRes['cover']),
      brand: Brand.fromJson(asT<Map<String, dynamic>>(jsonRes['brand'])),
      code: asT<String>(jsonRes['code']),
      layer: asT<String>(jsonRes['layer']),
      size: asT<String>(jsonRes['size']),
      biaomianjili: asT<String>(jsonRes['biaomianjili']),
      view_num: asT<int>(jsonRes['view_num']),
      like_num: asT<int>(jsonRes['like_num']),
      collect_num: asT<int>(jsonRes['collect_num']),
      related: related,
      name: asT<String>(jsonRes['name']),
      article: article,
      image: asT<String>(jsonRes['image']),
      story: asT<String>(jsonRes['story']),
      is_like: asT<int>(jsonRes['is_like']),
      is_collect: asT<int>(jsonRes['is_collect']),
    );
  }

  int id;
  Brand brand;
  String cover;
  String code;
  String layer;
  String size;
  String biaomianjili;
  int view_num;
  int like_num;
  int collect_num;
  List<MaterialItem> related;
  String name;
  List<InspirationInfo> article;
  String image;
  String story;
  int is_like;
  int is_collect;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
        'cover': cover,
        'code': code,
        'layer': layer,
        'size': size,
        'biaomianjili': biaomianjili,
        'view_num': view_num,
        'like_num': like_num,
        'collect_num': collect_num,
        'related': related,
        'name': name,
        'article': article,
        'image': image,
        'story': story,
        'is_like': is_like,
        'is_collect': is_collect,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class Brand {
  Brand({
    this.name,
    this.portrait,
  });

  factory Brand.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) return null;
    return Brand(
      name: asT<String>(jsonRes['name']),
      portrait: asT<String>(jsonRes['portrait']),
    );
  }

  String name;
  String portrait;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'name': name,
        'portrait': portrait,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
